package io.inputstream;

import org.junit.jupiter.api.Test;

import java.io.*;

/**
 * @author DELL
 * @create 2017-08-30 14:53
 */
public class Demo_02 {
    @Test
    public void testRead() {
        byte[] bytes = new byte[]{'a','A','3','4','5','a','%'};
        // ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);

        byte[] input = new byte[10];
        try(ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes)) {
            for(int i = 0; i< input.length; i++) {
                int b = inputStream.read();
                if(b == -1) break;
                input[i] = (byte)b;
            }
        } catch(IOException e) {
            e.printStackTrace();
        }

        for (byte b : input) {
            System.out.print(b + " ");
        }
    }

    @Test
    public void testRead2() throws IOException {
        InputStream in = new FileInputStream("src/io/inputstream/data.txt");
        int bytesRead = 0;
        int bytesToRead = 1024;
        byte[] input = new byte[bytesToRead];
        while(bytesRead < bytesToRead) {
            int result = in.read(input, bytesRead, bytesToRead - bytesRead);
            if(result == -1) break;
            bytesRead += result;
        }

        for (byte b : input) {
            System.out.print((char)b + " ");
        }
    }

}
